<?php
namespace Home\Controller;
use Think\Controller;
class DbController extends CommonController {
    public function index(){
        $this->display();
    }

    public function tbinfo(){
        $this->display();
    }

    public function creatC($name = '',$f ='')
    {
        $vo = F(C('ITEM_DIR').$f);
        $name = str_replace($vo['DB_PREFIX'], "", $name);
        $name = parse_name($name,2);
        \Common\Org\Build::buildController($name,$vo['ITEM_CONNTROLLER_PATH']);
        $this->success('操作成功');
    }

    public function creatM($f ='',$name = '')
    {
        if (!$name) {
            $this->error('模型名称不能为空');
        }
        $vo = F(C('ITEM_DIR').$f);
        $name = str_replace($vo['DB_PREFIX'], "", $name);
        $name = parse_name($name,2);
        // print_r($vo['ITEM_MODEL_PATH']);exit();
        \Common\Org\Build::buildModel($name,$vo['ITEM_MODEL_PATH']);
        $this->success('操作成功');
    }



    public function table($tb ='')
    {

        $Table = D('Table');
        $table = $Table->getInfo($tb);
        $this->assign('table_count', count($table)-1);
        $this->assign('table', $table);

        $this->display();
    }


    public function document($tb ='',$t = '')
    {
        if ($t == 'all') {
            $tbs = $this->getTables();
            $Model = M();
            $Table = D('Table');

            for ($i=0; $i < count($tbs); $i++) {
                $tb = $tbs[$i]['table_name'];
                $sql = $Model->query("show create table $tb;");
                $table = $Table->getInfo($tb);
                $tbs[$i]['sql'] = $sql[0]['create table'];
                $tbs[$i]['table'] = $table;
            }
            $this->assign('tbs', $tbs);
            $this->display('documentAll');
            exit();
        }

        $Table = D('Table');
        $table = $Table->getInfo($tb);
        // print_r($table);
        $this->assign('table', $table);

        $Model = M();
        $sql = $Model->query("show create table $tb;");
        $this->assign('sql', $sql[0]['create table']);

        $this->display();
    }


    public function opt($setup ='',$tb = '')
    {
        $Model = M();
        if ($setup == 'analyze') {
            $sql = "ANALYZE TABLE `".$tb."`;";
            $Model->execute($sql);
            $this->success($sql);
        }elseif ($setup == 'optimize') {
            $sql = "OPTIMIZE TABLE `".$tb."`;";
        }elseif ($setup == 'check') {
            $sql = "CHECK TABLE `".$tb."`;";
        }elseif ($setup == 'repair') {
            $sql = "REPAIR TABLE `".$tb."`;";
        }elseif ($setup == 'truncate') {
            $sql = "TRUNCATE TABLE `".$tb."`;";
        }
        if ($sql) {
            $Model->execute($sql);
            $this->success($sql);
        }else{
            $this->error('未做任何修改');
        }
    }

    public function copyFrom($scene ='',$source = 'Common')
    {
        if (IS_POST) {
            $i = F($this->tb,'',C('RUN_PATH'));
            $s = $i[$source];
            $i[$scene] = $s;
            F($this->tb,$i,C('RUN_PATH'));
            $this->success('已成功复制场景');
        }else{
            $this->display();
        }
    }

    public function design($scene ='Common')
    {
        $Table = D('Table');
        $table = $Table->getInfo($this->tb);
        $table = getTbFormField($table,$scene);
        // createTbFormField($table);
        $scenes = getAllFormScene($this->tb);
        $this->getTables();
        $this->assign('table', $table);
        $this->assign('scene', $scene);
        $this->assign('scenes', $scenes);
        $this->assign('table_count', count($table)-1);
        if (I('t')) {            
            $this->success('操作成功');
        }
        $this->display();
    }

    public function updateField($tb = '',$name ='',$pk ='',$value = '')
    {
        $Table = D('Table');
        if ($name == 'default') {
            $Table->saveFieldDef($tb,$pk,$value);
        }elseif ($name == 'isnull') {
            $Table->saveFieldIsNull($tb,$pk,$value);
        }elseif ($name == 'length') {
            $Table->saveFieldLength($tb,$pk,$value);
        }elseif ($name == 'moveUp' ) {
            $Table->alter($tb)->shiftUp($pk);
        }elseif ( $name == 'moveDown') {
            $Table->alter($tb)->shiftDown($pk);
        }elseif ($name == 'comment') {
            $Table->saveFieldComment($tb,$pk,$value);
        }

    }

    public function updateFrom($tb = '',$name ='',$pk ='',$value = '',$scene = 'Common')
    {
        if ($name == 'comment') {
            $this->updateField($tb,$name,$pk,$value);
            exit();
        }


        $info = F($tb,'',C('ITEM_PATH'));
        if (!$info) $this->error('修改失败');
        $value = str_replace("：", ":", $value);
        $info[$scene][$pk][$name] = $value;
        F($tb,$info,C('ITEM_PATH'));


        $this->success('修改成功');
    }
    public function deleteFrom($tb = '',$name ='',$pk ='',$value = '')
    {
        if ($pk == 'all') {
            F(C('ITEM_PATH').$tb,NULL);
            $this->success('修改成功');
        }
        $info = F($tb,'',C('ITEM_PATH'));
        if (!$info) $this->error('修改失败');
        unset($info['Common'][$pk]);
        F(C('ITEM_PATH').$tb,$info);
        $this->success('修改成功');
    }

    public function addField($tb = '')
    {
        $Table = D('Table');
        if (IS_POST) {
            $Table->alter($tb)->add($_POST)->exec();
            $this->success('操作成功');
        }else {
            $fields = $Table->getInfo($tb);
            $this->assign('fields', $fields);
            $this->display();
        }
    }

    public function deleteField($tb ='',$field = '')
    {
        $Table->alter($tb)->drop($field);
        $this->success('操作成功');
    }
}
